package feng.demo.controller.mer;

import cn.dev33.satoken.stp.StpUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import feng.demo.entity.Order;
import feng.demo.entity.Orders;
import feng.demo.entity.User;
import feng.demo.entity.XJson;
import feng.demo.service.OrderService;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mer/order")
public class MerOrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;

    @GetMapping("/findAll/{p}/{l}")
    public String findAll(@PathVariable("p")Integer p, @PathVariable("l")Integer l){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        Map<String,Object> map=new HashMap<>();
        List<Orders> list = orderService.findByUidLimit(user.getUid(),p,l);
        map.put("order",list);
        return new XJson(200,"",map).toString();
    }

    @PostMapping("/findByOrder")
    public String findByOrder(Order order){
        List<Orders> orders = orderService.findOrders(order);

        return new XJson(200,"",orders).toString();
    }

    @PostMapping("/updateship/{p}/{l}")
    public String updateship(Order order,@PathVariable("p")Integer p, @PathVariable("l")Integer l){
        order.setTship(new Date());
        order.setShipment(1);
        orderService.update(order);
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        Map<String,Object> map=new HashMap<>();
        List<Orders> list = orderService.findByUidLimit(user.getUid(),p,l);
        map.put("order",list);
        return new XJson(200,"发货成功",map).toString();
    }


}
